package com.ruoyi.tb.lmt.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.GuidUtil;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbRepairplanVo;
import com.ruoyi.tb.lmt.domain.bo.TbRepairplanBo;
import com.ruoyi.tb.lmt.service.ITbRepairplanService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检修计划
 *
 * @author ruoyi
 * @date 2023-12-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/repairplan")
public class TbRepairplanController extends BaseController {

    private final ITbRepairplanService iTbRepairplanService;

    /**
     * 查询检修计划列表
     */
    @SaCheckPermission("xha-lmt:repairplan:list")
    @GetMapping("/list")
    public TableDataInfo<TbRepairplanVo> list(TbRepairplanBo bo, PageQuery pageQuery) {
        return iTbRepairplanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出检修计划列表
     */
    @SaCheckPermission("xha-lmt:repairplan:export")
    @Log(title = "检修计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbRepairplanBo bo, HttpServletResponse response) {
        List<TbRepairplanVo> list = iTbRepairplanService.queryList(bo);
        ExcelUtil.exportExcel(list, "检修计划", TbRepairplanVo.class, response);
    }

    /**
     * 获取检修计划详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xha-lmt:repairplan:query")
    @GetMapping("/{id}")
    public R<TbRepairplanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTbRepairplanService.queryPlanAndListById(id));
    }

    /**
     * 新增检修计划
     */
    @SaCheckPermission("xha-lmt:repairplan:add")
    @Log(title = "检修计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbRepairplanBo bo) {
        Long planCode = GuidUtil.getZcbhid();
        bo.setPlanCode(planCode.toString());
        return toAjax(iTbRepairplanService.insertPlanAndListByBo(bo));
    }

    /**
     * 修改检修计划
     */
    @SaCheckPermission("xha-lmt:repairplan:edit")
    @Log(title = "检修计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbRepairplanBo bo) {
        return toAjax(iTbRepairplanService.updatePlanAndListByBo(bo));
    }

    /**
     * 删除检修计划
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xha-lmt:repairplan:remove")
    @Log(title = "检修计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTbRepairplanService.deletePlanAndListByIds(Arrays.asList(ids), true));
    }
    @SaCheckPermission("xha-lmt:repairplan:issued")
    @Log(title = "检修计划状态更改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/setStatus")
    public R<Void> setStatus(@Validated(EditGroup.class) @RequestBody TbRepairplanBo bo) {
        // 作废:状态变为“作废”,该计划下的检修任务也均变为已作废(无视任务状态)
        // 计划下发：状态变为正式计划，计划清单变为新计划
        return toAjax(iTbRepairplanService.updatePlanAndListStatusByBo(bo));
    }


//    @SaCheckPermission("xha-lmt:repairplan:todoCreateWorkList")
//    @GetMapping("/listByStatus/todoCreateWorkList")
//    public TableDataInfo<TbRepairplanVo> todoCreateWorkList(TbRepairplanBo bo, PageQuery pageQuery) {
//        return iTbRepairplanService.queryPagePlanAndList(bo, pageQuery);
//    }
    //导出甘特图
    @SaCheckPermission("xha-lmt:repairplan:export")
    @RequestMapping(value = "/excel/export")
    public void export(HttpServletRequest request, HttpServletResponse response, Long id){
        iTbRepairplanService.export(request,response,id);
    }

    /**
     * 导出初始计划
     */
    @SaCheckPermission("xha-lmt:repairplan:export1")
    @RequestMapping(value = "/excel/export1")
    public void export1(HttpServletRequest request, HttpServletResponse response, Long id){
        iTbRepairplanService.export1(request,response,id);
    }

    /**
     * 查询最新一次的计划
     */

    @GetMapping("/nowList")
    public TbRepairplanVo newList() {
        return iTbRepairplanService.nowList();
    }

    /**
     * 查询全部的计划
     */

    @GetMapping("/allList")
    public List<TbRepairplanVo> allList() {
        return iTbRepairplanService.allList();
    }


}
